package com.wangyi.fightimg.widget.sharepopup;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.tauth.IUiListener;
import com.wangyi.fightimg.R;
import com.wangyi.fightimg.base.BasePopupWindow;
import com.wangyi.fightimg.utils.Constants;
import com.wangyi.fightimg.utils.ShareUtil;

import java.util.ArrayList;
import java.util.List;


/**
 * @author wangYi
 *         分享PopupWindow
 */

public class SharePopupWindow extends BasePopupWindow implements SharePopupAdapter.OnItemClickListener {
    private RecyclerView rvShare;// 分享平台列表
    private SharePopupAdapter adapter;// 列表适配器
    private List<SharePlatform> sharePlatforms = new ArrayList<>();// 列表项数据集合
    private String imgUri;// 图片本地地址
    private IWXAPI api;
    private IUiListener iUiListener;// qq分享回调监听
    private Context context;

    public SharePopupWindow(Context context, IWXAPI api, IUiListener iUiListener) {
        super(context, R.layout.popup_share, R.id.ll_share_root);
        this.api = api;
        this.context = context;
        this.iUiListener = iUiListener;

        rvShare = (RecyclerView) view.findViewById(R.id.rv_share);
        // 添加列表项数据
        sharePlatforms.add(createSharePlatform(R.mipmap.ssdk_oks_classic_qq, "QQ好友"));
        sharePlatforms.add(createSharePlatform(R.mipmap.ssdk_oks_classic_wechat, "微信好友"));
        sharePlatforms.add(createSharePlatform(R.mipmap.ssdk_oks_classic_sinaweibo, "新浪微博"));
        sharePlatforms.add(createSharePlatform(R.mipmap.ssdk_oks_classic_qzone, "QQ空间"));
        sharePlatforms.add(createSharePlatform(R.mipmap.ssdk_oks_classic_wechatmoments, "微信朋友圈"));
        // 创建列表
        GridLayoutManager gridLayoutManager = new GridLayoutManager(context, 4);
        rvShare.setLayoutManager(gridLayoutManager);
        adapter = new SharePopupAdapter(context, sharePlatforms, this);
        rvShare.setAdapter(adapter);
        rvShare.setHasFixedSize(true);
    }

    /**
     * 创建 SharePlatform
     *
     * @param platformId
     * @param platformName
     */
    private SharePlatform createSharePlatform(int platformId, String platformName) {
        SharePlatform sharePlatform = new SharePlatform();
        sharePlatform.setPlatformLog(platformId);
        sharePlatform.setPlatformName(platformName);
        return sharePlatform;
    }


    @Override
    public void setOnItemClick(int log) {
        switch (log) {
            case R.mipmap.ssdk_oks_classic_qq:// QQ好友
                ShareUtil.shareToQQ(context, imgUri, iUiListener);
                break;
            case R.mipmap.ssdk_oks_classic_wechat:// 微信好友
                if (api.isWXAppInstalled()) {
                    ShareUtil.shareToWechat(api, imgUri);
                } else {
                    Toast.makeText(context, "请先安装微信客户端", Toast.LENGTH_LONG).show();
                }
                break;
            case R.mipmap.ssdk_oks_classic_sinaweibo:// 新浪微博
                ShareUtil.shareToSinaweibo(imgUri);
                break;
            case R.mipmap.ssdk_oks_classic_qzone:// QQ空间
                ShareUtil.shareToQZone(context, imgUri, iUiListener);
                break;
            case R.mipmap.ssdk_oks_classic_wechatmoments:// 微信朋友圈
                // 是否安装客户端
                if (api.isWXAppInstalled()) {
                    int wxSdkVersion = api.getWXAppSupportAPI();
                    // 判断当前微信版本是否支持分享功能呢个
                    if (wxSdkVersion >= Constants.WX_SDK_VERSION) {
                        ShareUtil.shareToWechatMoments(api, imgUri);
                    } else {
                        Toast.makeText(context, "当前微信版本不支持分享到朋友圈", Toast.LENGTH_LONG).show();
                    }
                } else {
                    Toast.makeText(context, "请先安装微信客户端", Toast.LENGTH_LONG).show();
                }
                break;
            default:
                break;
        }
        dismiss();
    }

    public void setImgUri(String imgUri) {
        this.imgUri = imgUri;
    }
}
